import { defineStore } from "pinia";

export const useConfigStore = defineStore("config", {
  state: () => ({
    theme: localStorage.getItem("theme") || "light",
    language: localStorage.getItem("language") || "zh-CN",
    sidebarCollapsed:
      localStorage.getItem("sidebarCollapsed") === "true" || false,
  }),
  getters: {
    isDarkMode: (state) => state.theme === "dark",
  },
  actions: {
    toggleTheme() {
      this.theme = this.theme === "light" ? "dark" : "light";
      localStorage.setItem("theme", this.theme);

      // 切换ElementPlus的暗黑模式
      if (this.theme === "dark") {
        document.documentElement.classList.add("dark");
      } else {
        document.documentElement.classList.remove("dark");
      }
    },
    setLanguage(lang) {
      this.language = lang;
      localStorage.setItem("language", lang);
    },
    toggleSidebar() {
      this.sidebarCollapsed = !this.sidebarCollapsed;
      localStorage.setItem("sidebarCollapsed", this.sidebarCollapsed);
    },
  },
});
